// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <nebula/types/util.h>
#include <nebula/types/type.h>

namespace nebula::internal {

    std::unordered_multimap<std::string, int> create_name_to_index_map(
            const FieldVector &fields) {
        std::unordered_multimap<std::string, int> name_to_index;
        for (size_t i = 0; i < fields.size(); ++i) {
            name_to_index.emplace(fields[i]->name(), static_cast<int>(i));
        }
        return name_to_index;
    }

    void append_metadata_finger_print(const KeyValueMetadata &metadata,
                                          std::stringstream *ss) {
        // Compute metadata fingerprint.  KeyValueMetadata is not immutable,
        // so we don't cache the result on the metadata instance.
        const auto pairs = metadata.sorted_pairs();
        if (!pairs.empty()) {
            *ss << "!{";
            for (const auto &p: pairs) {
                const auto &k = p.first;
                const auto &v = p.second;
                // Since metadata strings can contain arbitrary characters, prefix with
                // string length to disambiguate.
                *ss << k.length() << ':' << k << ':';
                *ss << v.length() << ':' << v << ';';
            }
            *ss << '}';
        }
    }

}  // namespace nebula::internal
